//
//  StatusCell.swift
//  swift_learning
//
//  Created by aron on 2017/2/2.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class StatusCell: UITableViewCell {

    /// 头像
    @IBOutlet weak var avatarImgView: UIImageView!
    
    /// 认证
    @IBOutlet weak var verifiedIcon: UIImageView!
    
    /// 昵称
    @IBOutlet weak var nicknameLabel: UILabel!
    
    /// vip等级
    @IBOutlet weak var vipIcon: UIImageView!
    
    /// 时间
    @IBOutlet weak var timeLabel: UILabel!
    
    /// 来源
    @IBOutlet weak var sourceLabel: UILabel!
    
    /// 内容
    @IBOutlet weak var contentTextLabel: UILabel!
    
    /// 转发的微博的内容
    @IBOutlet weak var retweetContentTextLabel: UILabel!
    
    /// 微博配图CollectionView
    @IBOutlet weak var statusPictureView: StatusPictureView!
    
    /// 转发微博的背景
    @IBOutlet weak var retweetStatusBgView: UIView!
    
    
    // MARK: - ......::::::: 约束 :::::::......
    @IBOutlet weak var contentTextWCons: NSLayoutConstraint!
    @IBOutlet weak var statusPicViewWCons: NSLayoutConstraint!
    @IBOutlet weak var statusPicViewHCons: NSLayoutConstraint!
    
    @IBOutlet weak var retweetedStatusContentBottomCons: NSLayoutConstraint!
    @IBOutlet weak var statusPictureViewBottomCons: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        let edgePaddingH : CGFloat = 15;
        
        contentTextWCons.constant = SCREEN_WIDTH() - edgePaddingH*2;
        
        avatarImgView.layer.cornerRadius = 20;
        avatarImgView.layer.masksToBounds = true
        
        statusPictureView.isScrollEnabled = false
        statusPictureView.showsVerticalScrollIndicator = false
        statusPictureView.showsHorizontalScrollIndicator = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    public func setStatus(_ statusVM: StatusViewModel) {
        // 设置头像
        avatarImgView.sd_setImage(with: statusVM.avatarURL, placeholderImage: DEFAULT_AVATAR_SMALL())
        
        // 认证
        verifiedIcon.image = statusVM.verifiedImage
        
        // 昵称
        nicknameLabel.text = statusVM.status?.user?.screen_name
        
        // vip等级
        vipIcon.image = statusVM.vipImage
        
        // 时间
        timeLabel.text = statusVM.createdAtText
        
        // 来源
        sourceLabel.text = statusVM.sourceText
        
        // 内容
        contentTextLabel.text = statusVM.status?.text
        
        // 被转发微博的内容
        if let retweetedScreenName = statusVM.status?.retweeted_status?.user?.screen_name,
            let retweetedText = statusVM.status?.retweeted_status?.text{
            retweetContentTextLabel.text = "@" + "\(retweetedScreenName): " + retweetedText
        }else {
            retweetContentTextLabel.text = nil
        }
        
        // 配图View大小
        let picViewSize = calculatePictureViewSize(pictureCount: statusVM.picUrls.count)
        statusPicViewWCons.constant = picViewSize.width
        statusPicViewHCons.constant = picViewSize.height
        
        // 设置配图
        statusPictureView.setPicUrls(picUrls: statusVM.picUrls)
        
        // 设置转发微博的底部约束
        // 设置显示隐藏转发微博的背景
        if let retweeted_status = statusVM.status?.retweeted_status {
            if retweeted_status.text != nil {
                retweetedStatusContentBottomCons.constant = 8
            }else {
                retweetedStatusContentBottomCons.constant = 0
            }
            
            retweetStatusBgView.isHidden = false
        }else {
            retweetedStatusContentBottomCons.constant = 0
            
            retweetStatusBgView.isHidden = true
        }
        
        // 设置配图的底部约束
        if statusVM.picUrls.count > 0 {
            statusPictureViewBottomCons.constant = 8
        }else {
            statusPictureViewBottomCons.constant = 0
        }
        
    }
    
    
    /// 计算配图View的大小
    ///
    /// - Parameter pictureCount: 图片张数
    /// - Returns: 配图View的大小
    private func calculatePictureViewSize(pictureCount: Int) -> CGSize {
        
        let edgeHPadding : CGFloat = 15;
        let itemHPadding : CGFloat = 10;
        
        let itemWH : CGFloat = (SCREEN_WIDTH()-edgeHPadding*2-itemHPadding*2)/3
        
        // 设置CollectionView的ItemSize
        let layout : UICollectionViewFlowLayout = statusPictureView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        
        if pictureCount == 0 {
            // 没有图片
            return CGSize(width: 0, height: 0)
        } else if pictureCount == 4 {
            // 四张使用田字格布局
            return CGSize(width: itemWH*2+itemHPadding, height: itemWH*2+itemHPadding)
        } else {
            // 其他张数
            let row = (pictureCount-1)/3+1;
            return CGSize(width: SCREEN_WIDTH()-edgeHPadding*2, height: itemWH*CGFloat(row)+itemHPadding*CGFloat((row-1)))
        }
    }
    
}
